#pragma once
#include <string>
#include <fstream>
#include <rapidjson/document.h>
#include "logger.h"
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <sstream>
#include <iostream>
#include "ScalarStorage.h"
#include "index_factory.h"
class Persistence{
public:
    Persistence():increaseID_(0){};
    ~Persistence();

    void init(const std::string& path);
    uint64_t increaseID();
    uint64_t getID()const;
    void writeWALLog(const std::string& operation_type,const rapidjson::Document& json_data,const std::string& version);
    void writeWALRawLog(uint64_t log_id,const std::string& operation_type,const std::string& raw_data,const std::string &version);
    void readNextWALLog(std::string *operation_type,rapidjson::Document* json_data);

    //快照
    void takeSnapshot(ScalarStorage& scalarStorage);
    void loadSnapshot(ScalarStorage& scalarStorage);
    void saveLastSnapshotID();
    void loadLastSnapshotID();
private:
    uint64_t increaseID_;
    uint64_t lastSnapshotID;
    std::fstream wal_log_file_;
};